﻿using System.Linq;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Evento.Models;

namespace Evento.Controllers
{
    public class EventoWebServiceController : Controller
    {
		#region Fields 

        private readonly EventoDB _eventoDB;

		#endregion Fields 

		#region Constructors 

        /// <summary>
        /// Initializes a new instance of the <see cref="EventoWebServiceController"/> class.
        /// </summary>
        public EventoWebServiceController()
        {
            _eventoDB = new EventoDB();
        }

		#endregion Constructors 

		#region Methods 

        public string GetPlacesList()
        {
            var eventsList = _eventoDB.Places.ToList();

            var javaScriptSerializer = new JavaScriptSerializer();

            return javaScriptSerializer.Serialize(eventsList);
        }

        /// <summary>

        /// </summary>
        /// <returns> link of event in place of guid</returns>
        public string CheckEventInPlace(string guid)
        {
            var javaScriptSerializer = new JavaScriptSerializer();

            foreach (var ev in _eventoDB.Events)
            {
                if (ev.PlaceGuid.Equals(guid))
                {
                    //todo: return number of events, regarding time it occurs (now)
                    return javaScriptSerializer.Serialize(ev);
                }
            }

            return javaScriptSerializer.Serialize(string.Empty);
        }

        /// <summary>
        /// GET: /EventoWebService/ 
        /// </summary>
        /// <returns></returns>
        public string Index()
        {
            var eventsList = _eventoDB.Events.ToList();

            var javaScriptSerializer = new JavaScriptSerializer();

            return javaScriptSerializer.Serialize(eventsList);
        }

		#endregion Methods 
    }
}